package com.hj.ct.logic;

import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hj.ct.App;
import com.hj.ct.db.dao.DeviceDao;
import com.hj.ct.db.dao.SetAlarmDao;
import com.hj.ct.db.dao.UserDao;
import com.hj.ct.http.StringHttpResponseListener;
import com.hj.ct.model.SetAlarm;
import com.hj.ct.model.User;
import com.hj.ct.parse.Device;
import com.hj.ct.util.LogF;
import com.hj.ct.util.SharePreferenceUtil;

public class LoginLogic extends BaseLogic {
	private SetAlarmDao alarmDao;
	private DeviceDao deviceDao;
	private UserDao userDao;
	public static String APLYING = "applying";
	public static String VIP = "vip";
	private long serverTime = System.currentTimeMillis();
	private static LoginLogic loginLogic;

	public long getServerTime() {
		return serverTime;
	}

	public void setServerTime(long serverTime) {
		this.serverTime = serverTime;
	}

	private LoginLogic() {
		alarmDao = new SetAlarmDao(App.getContext());
		deviceDao = new DeviceDao(App.getContext());
		userDao = new UserDao(App.getContext());
	}

	public static LoginLogic getInstance() {
		if (loginLogic == null) {
			loginLogic = new LoginLogic();
		}
		return loginLogic;
	}

	public void login(final OnHandlerResponse handler, final String userName, final String password) {

		httpUtil.get(HttpCommon.LOGIN + "&user=" + userName + "&pass=" + password, new StringHttpResponseListener() {

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				// TODO Auto-generated method stub
				LogF.d("LoginLogic", "login error:" + content);
				handler.handlerResponse(LogicActions.LOGIN_ERROR, statusCode, content);
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				LogF.d("LoginLogic", "login statusCode:" + statusCode + ",content:" + content);
				// 成功statusCode:200,失败202
				if (statusCode != 200) {
					handler.handlerResponse(LogicActions.LOGIN_ERROR, statusCode, content);
					return;
				}
				userDao.deleteUser(userName);
				userDao.insertUser(new User(userName, password));
				SharePreferenceUtil.getInstance(App.getContext()).setString("currentUserName", userName);
				List<Device> devices = null;
				try {
					devices = new Gson().fromJson(content, new TypeToken<List<Device>>() {
					}.getType());
				} catch (Exception e) {

				}
				if (devices != null) {
					deviceDao.deleteDevices();
					deviceDao.insertDevices(devices);
					handler.handlerResponse(LogicActions.LOGIN_SUCCESS, statusCode, devices);
				} else {
					handler.handlerResponse(LogicActions.LOGIN_ERROR, statusCode, content);
				}
				getAlarm(new OnHandlerResponse() {
					public void handlerResponse(int action, int code, Object... obj) {

					}
				});
				if (App.app != null) {
					App.app.startTask();
				}
			}
		});
	}

	public void getAlarm(final OnHandlerResponse handler) {
		httpUtil.get(HttpCommon.GETSETTING, new StringHttpResponseListener() {
			public void onFailure(int statusCode, String content, Throwable error) {
				LogF.d("getAlarm", "getAlarm statusCode:" + statusCode + ",content:" + content);
				handler.handlerResponse(LogicActions.GET_ERROR, statusCode, content);
			}

			public void onSuccess(int statusCode, String content) {
				LogF.d("getAlarm", "getAlarm statusCode:" + statusCode + ",content:" + content);
				if (statusCode == 202) {
					handler.handlerResponse(LogicActions.NEED_TO_LOGIN, statusCode, content);
				} else if (statusCode == 200) {
					List<SetAlarm> alarm = null;
					try {
						alarm = new Gson().fromJson(content, new TypeToken<List<SetAlarm>>() {
						}.getType());
					} catch (Exception e) {

					}
					alarmDao.deleteAlarm();
					alarmDao.insertSet(alarm);
					handler.handlerResponse(LogicActions.GET_SUCCESS, statusCode, alarm);

					return;
				}
				handler.handlerResponse(LogicActions.GET_ERROR, statusCode, content);
			}
		});
	}

	public HashMap<String, SetAlarm> getSetAlarm() {
		return alarmDao.selectAlarms();
	}

	public SetAlarm getSetAlarm(String devId) {
		return alarmDao.selectAlarms(devId);
	}

	public void logout(final OnHandlerResponse handler) {

		httpUtil.get(HttpCommon.LOGOUT, new StringHttpResponseListener() {
			String userName = SharePreferenceUtil.getInstance(App.getContext()).getString("currentUserName", "");

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				// TODO Auto-generated method stub
				LogF.d("LoginLogic", "logout error:" + content);
				handler.handlerResponse(LogicActions.LOGOUT_ERROR, statusCode, content);
				userDao.deleteUser(userName);
				if (App.app != null) {
					App.app.destroyTask();
				}
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				LogF.d("LoginLogic", "logout statusCode:" + statusCode + ",content:" + content);
				handler.handlerResponse(LogicActions.LOGOUT_SUCCESS, statusCode, content);
				userDao.deleteUser(userName);
				if (App.app != null) {
					App.app.destroyTask();
				}
			}
		});
	}

}
